/// <reference path="../utilities/FXDom.js" />
/// <reference path="../utilities/Xpcom.js" />
///--------------------------------------------------------------------
/// <file name="Thread.js">
///   Provides information about a thread on Neoseeker.
/// </file>
///--------------------------------------------------------------------

NAMFox.ns(function() { with (NAMFox) {

this.Thread = function(_$, forumId, threadId, subject, serverHtml) {
    /// <summary>
    ///   Initializes a new instance of the Thread class.
    /// </summary>
    /// <param name="_$" type="FXDom">
    ///   Facilitates DOM traversal.
    /// </param>
    /// <param name="forumId" type="Number" integer="true">
    ///   The ID of the forum in which the thread resides.
    /// </param>
    /// <param name="threadId" type="Number" integer="true">
    ///   The ID of the current thread.
    /// </param>
    /// <param name="subject" type="String">
    ///   The subject of the thread.
    /// </param>
    /// <param name="serverHtml" type="String">
    ///   The HTML sent by the server, NOT the HTML rendered in Firefox's DOM.
    /// </param>
    
    this.waitHandle = new ManualResetEvent();
    this.forumId = forumId;
    this.subject = subject;
    this._serverHtml = serverHtml;
    this._$ = _$;
    this.threadId = threadId;
}

Thread.subjectNormalizerExpr = /^(?:re: )*(.*)$/;

Thread.prototype = {
    _ajaxServerHtml: null,
    _isModerator: null,
    _posts: null,
    _serverHtml: null,
    forumId: null,
    subject: null,
    threadId: null,
    waitHandle: null,
    
    _attachServerHtml: function(serverHtml) {
        /// <summary>
        ///   Called by the AJAX callback to associate the raw HTML of
        ///   the page with this Thread object.
        /// </summary>
        /// <param name="serverHtml" type="String">
        ///   The HTML, as sent by the server, for this page.
        /// </param>
        
        this._ajaxServerHtml = serverHtml;
        this._populatePosts();
        this.waitHandle.set();
    },
    
    _populatePosts: function() {
        /// <summary>
        ///   Populates each of the Post objects with its respective markup.
        /// </summary>
        
        if (!this._serverHtml && this._posts && this._ajaxServerHtml) {
            var posts = this._posts;
            for (var i = 0; i < posts.length; ++i) {
                var post = posts[i];
                var postId = post.getMessageId();
                
                post.setHtml(this._ajaxServerHtml);
            }
        }
    },
    
    getPosts: function() {
        /// <summary>
        ///   Gets all of the posts associated with the current page.
        /// </summary>
        /// <returns type="Array" />
        
        if (!this._posts) {
            var posts = this._posts = [];
            
            var that = this;
            this._$("td.authorcol").parent().each(
                function(i, element) {
                    var post = new Post(element);
                    
                    if (that._serverHtml) {
                        post.setHtml(that._serverHtml);
                    }
                    posts.push(post);
                }
            );
            this._populatePosts();
        }
        
        return this._posts;
    },

    getSubjectForReply: function() {
        /// <summary>
        /// Gets the subject of the thread in a manner that can be used for posting.
        /// </summary>
        /// <returns type="String" />
        
        return this.subject.replace(Thread.subjectNormalizerExpr, "re: $1");
    },
    
    getUrl: function() {
        /// <summary>
        /// Gets the URL of this thread.
        /// </summary>
        
        return $.url.forum(this.forumId + "/t" + this.threadId + "-/");
    },
    
    isInBugForum: function() {
        /// <summary>
        /// Gets whether this thread exists in a bug forum.
        /// </summary>
        
        return this._$("#bug_status").length > 0;
    },

    isUserModerator: function() {
        /// <summary>
        ///   Determines whether the current user is a moderator
        ///   who has power over the current thread.
        /// </summary>
        /// <returns type="Boolean" />
        
        if (this._isModerator === null) {
            this._isModerator = this._$("input[value=mass_message_change]").any();
        }
        
        return this._isModerator;
    }
};

}});